<template>
    <div class="login">
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="loginname" name="loginname" label="用户名" placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    登录
                </van-button>
            </div>
        </van-form>

        <div>
            <router-link to="/register">用户注册</router-link>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
// import { login } from '@/api/login-register'
import { showToast } from 'vant';
import { useRouter } from 'vue-router';
import useUserStore from '@/store/user'

const router = useRouter()

const store = useUserStore()

const loginname = ref('17586688668');
const password = ref('666666');

// const onSubmit = async (/*values: any*/) => {
//     // console.log('submit', values);
//     try {
//         const result = await login(loginname.value, password.value)
//         console.log('登录结果：', result)
//         router.push('/mine')
//     } catch (error: any) {
//         showToast({
//             type: 'fail',
//             message: error.message,
//         })
//     }
// };


const onSubmit = async () => {
    const result = await store.loginAction(loginname.value, password.value)
    if (result) {
        router.push('/mine')
    } else {
        showToast({
            type: 'fail',
            message: '用户名或密码错误',
        })
    }
}
</script>

<style lang="less" scoped></style>
